//
//  MeInfoVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/29.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "MeInfoVC.h"

@interface MeInfoVC ()<UITextFieldDelegate>

@property (nonatomic ,strong) UIView *topV;

@property (nonatomic ,strong) UIView *bottomV;

@property (nonatomic ,strong) UILabel *sexL;
@property (nonatomic ,strong) UILabel *nickL;
@property (nonatomic ,strong) UILabel *signL;

@property (nonatomic ,strong) UIButton *maleBtn;
@property (nonatomic ,strong) UIButton *remaleBtn;

@property (nonatomic ,strong) UITextField *nickTf;
@property (nonatomic ,strong) UITextField *signTf;

@end

@implementation MeInfoVC

- (void)loadView
{
    [super loadView];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人资料";
    
    [self buildNavRightItemWithTitle:@"保存" action:@selector(saveClick:)];
    [self buildNavBackItemWithAction:@selector(backClick:)];
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.showsVerticalScrollIndicator = NO;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.backgroundColor = xlHexColor(@"#f4f6f9");
    [self.view addSubview:scroll];
    
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = xlHexColor(@"#f4f6f9");
    [scroll addSubview:bgV];
    
    [bgV addSubview:self.topV];
    [bgV addSubview:self.bottomV];
    
    [scroll makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [bgV makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(0);
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        make.bottom.equalTo(self.bottomV.bottom).offset(30);
    }];
    
    [self.topV makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(0);
        make.bottom.equalTo(self.sexL.bottom);
    }];
    
    [self.bottomV makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo(self.topV.bottom).offset(30);
        make.bottom.equalTo(self.signL.bottom);
    }];
//    [self.signTf becomeFirstResponder];
}

- (void)backClick:(UIButton *)btn
{
    
    if (![self regexInfo]) {
        NSString *title = @"提示";
        NSString *message = @"您是否要放弃编辑";
        NSString *cancelButtonTitle = @"是的";
        NSString *otherButtonTitle = @"保存";
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];

        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self saveClick:nil];

        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        [self presentViewController:alertController animated:YES completion:nil];

        
    }else{
        [self.navigationController popViewControllerAnimated:YES];

    }
    
}

- (BOOL)regexInfo{
    
    NSString * male = self.maleBtn.selected?@"1":@"2";
    
    if (![[AccountHanler personalizedSignature] isEqualToString:self.signTf.text]||![[AccountHanler nickName] isEqualToString:self.nickTf.text]||![[AccountHanler sex] isEqualToString:male]) {
        
        return NO;
    }
    return YES;
}

- (void)saveClick:(UIButton *)btn
{
        NSDictionary *params = @{
                                 @"type": @"OSWIFI_USERINFO_MOBILE",
                                 @"token":[AccountHanler token],
                                 @"requestEntity": @{
                                         @"telephone":[AccountHanler telPhone],
                                         @"method":@"operate",
                                         @"username":[AccountHanler username],
                                         @"nickName":IsNull(self.nickTf.text, @""),
                                         @"sex":self.remaleBtn.selected?@"2":@"1",
                                         @"personalizedSignature":IsNull(self.signTf.text, @""),
                                         }
                                 };
        [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
            XLBaseModel *result = [XLBaseModel mj_objectWithKeyValues:returnData];
            if (result.resultFlag) {
                [self.view makeToast:@"信息保存成功"];
            }else{
                DLog(@"用户信息获取失败");
                [self.view makeToast:@"信息保存失败"];

            }
        } failureBlock:^(NSError *error) {
            DLog(@"用户信息获取失败");
            [self.view makeToast:@"信息保存失败"];

        } showHUD:NO];
        
}


- (void)maleClick:(UIButton *)btn
{
    if (!btn.selected) {
        btn.selected = YES;
        self.remaleBtn.selected = NO;
    }
}

- (void)remaleClick:(UIButton *)btn
{
    if (!btn.selected) {
        btn.selected = YES;
        self.maleBtn.selected = NO;
    }
}


- (UIView *)topV
{
    if (!_topV) {
        _topV = [[UIView alloc] init];
        _topV.backgroundColor = [UIColor whiteColor];
        
        [_topV addSubview:self.nickL];
        [_topV addSubview:self.nickTf];
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = xlRGB(201, 201, 201);
        [_topV addSubview:line];
        [_topV addSubview:self.sexL];
        [_topV addSubview:self.remaleBtn];
        [_topV addSubview:self.maleBtn];
        
        if ([[AccountHanler sex] isEqualToString:@"1"]) {
            self.remaleBtn.selected = NO;
            self.maleBtn.selected = YES;
        }else{
            self.remaleBtn.selected = YES;
            self.maleBtn.selected = NO;
        }

        
        [self.nickL makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.height.equalTo(55);
            make.top.equalTo(0);
            make.width.equalTo(80);
        }];
        [self.nickTf makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nickL.right).offset(20);
            make.right.equalTo(-15);
            make.top.equalTo(0);
            make.height.equalTo(55);
        }];
        
        [line makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(0);
            make.top.equalTo(self.nickL.bottom);
            make.height.equalTo(1);
        }];
        
        [self.sexL makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.height.equalTo(55);
            make.top.equalTo(line.bottom);
            make.width.equalTo(80);
            
        }];
        
        [self.maleBtn makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.sexL.right).offset(20);
            make.width.equalTo(50);
            make.top.equalTo(line.bottom);
            make.height.equalTo(55);
        }];
        
        [self.remaleBtn makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.maleBtn.right).offset(20);
            make.width.equalTo(50);
            make.top.equalTo(line.bottom);
            make.height.equalTo(55);
        }];
    }
    return _topV;
}

- (UIView *)bottomV
{
    if (!_bottomV) {
        _bottomV = [[UIView alloc] init];
        _bottomV.backgroundColor = [UIColor whiteColor];
        [_bottomV addSubview:self.signL];
        [_bottomV addSubview:self.signTf];
        
        [self.signL makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.height.equalTo(55);
            make.top.equalTo(0);
            make.width.equalTo(80);
        }];
        [self.signTf makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.signL.right).offset(20);
            make.right.equalTo(-15);
            make.top.equalTo(0);
            make.height.equalTo(55);
        }];
        
        
    }
    return _bottomV;
}

- (UITextField *)signTf
{
    if (!_signTf) {
        _signTf = [[UITextField alloc] init];
        _signTf.textColor = xlRGB(31, 31, 31);
        _signTf.font = xlFont(15);
        _signTf.delegate = self;
        _signTf.text = [AccountHanler personalizedSignature];
    }
    return _signTf;
}

- (UITextField *)nickTf
{
    if (!_nickTf) {
        _nickTf = [[UITextField alloc] init];
        _nickTf.textColor = xlRGB(31, 31, 31);
        _nickTf.font = xlFont(15);
        _nickTf.delegate = self;
        _nickTf.text = [AccountHanler nickName];
    }
    return _nickTf;
}

- (UILabel *)sexL
{
    if (!_sexL) {
        _sexL = [[UILabel alloc] init];
        _sexL.text = @"性别";
        _sexL.textColor = xlRGB(121, 121, 121);
        _sexL.font = xlFont(15);
    }
    return _sexL;
}

- (UILabel *)nickL
{
    if (!_nickL) {
        _nickL = [[UILabel alloc] init];
        _nickL.text = @"昵称";
        _nickL.textColor = xlRGB(121, 121, 121);
        _nickL.font = xlFont(15);
    }
    return _nickL;
}

- (UILabel *)signL
{
    if (!_signL) {
        _signL = [[UILabel alloc] init];
        _signL.text = @"个性签名";
        _signL.textColor = xlRGB(121, 121, 121);
        _signL.font = xlFont(15);
    }
    return _signL;
}

- (UIButton *)maleBtn
{
    if (!_maleBtn) {
        _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_maleBtn setTitle:@"男" forState:UIControlStateNormal];
        [_maleBtn setTitle:@"男" forState:UIControlStateSelected];
        [_maleBtn setTitleColor:xlRGB(31, 31, 31) forState:UIControlStateNormal];
        [_maleBtn setTitleColor:xlRGB(31, 31, 31)  forState:UIControlStateSelected];
        [_maleBtn setImage:[UIImage imageNamed:@"home_comment_sexChoose"] forState:UIControlStateNormal];
        [_maleBtn setImage:[UIImage imageNamed:@"home_comment_sexChoose_selected"] forState:UIControlStateSelected];
        [_maleBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
        [_maleBtn addTarget:self action:@selector(maleClick:) forControlEvents:UIControlEventTouchUpInside];
        _maleBtn.selected = YES;
    }
    return _maleBtn;
}

- (UIButton *)remaleBtn
{
    if (!_remaleBtn) {
        _remaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_remaleBtn setTitle:@"女" forState:UIControlStateNormal];
        [_remaleBtn setTitle:@"女" forState:UIControlStateSelected];
        [_remaleBtn setTitleColor:xlRGB(31, 31, 31) forState:UIControlStateNormal];
        [_remaleBtn setTitleColor:xlRGB(31, 31, 31) forState:UIControlStateSelected];
        [_remaleBtn setImage:[UIImage imageNamed:@"home_comment_sexChoose"] forState:UIControlStateNormal];
        [_remaleBtn setImage:[UIImage imageNamed:@"home_comment_sexChoose_selected"] forState:UIControlStateSelected];
        [_remaleBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
        [_remaleBtn addTarget:self action:@selector(remaleClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _remaleBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
